package com.qf.flash.ui.circle;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.qf.flash.R;
import com.qf.flash.http.response.PlayTypeBean;
import com.qf.flash.http.response.StrategyTopBean;
import com.qf.flash.model.StrategyBean;
import com.qf.flash.mvp.contract.StrategyContract;
import com.qf.flash.mvp.presenter.StrategyPresenter;
import com.qf.flash.ui.home.adapter.CircleRecommendItem;
import com.qf.flash.ui.home.adapter.StrategyTopItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.LazyBaseFragment;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;

/**
 * 圈子模块  ---->  攻略
 */
public class StrategyFragment extends LazyBaseFragment implements StrategyContract.View {

    @BindView(R.id.rv)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    private RVAdapter<Object> mAdapter;
    private StrategyPresenter strategyPresenter;
    private int pageNo = 1;
    private boolean isRefresh;
    private StrategyTopBean bean = new StrategyTopBean();

    @Override
    protected int setContentView() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initView() {
        strategyPresenter.getTypeLiveData().observe(this, playTypeBeans -> {
            List<Object> datas = mAdapter.getDatas();
            bean.setPlayTypeBeans(playTypeBeans);
            datas.add(0, bean);
            mAdapter.notifyDataSetChanged();
        });
        mRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                isRefresh = false;
                requestData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                isRefresh = true;
                requestData();
            }
        });
    }
    private String typeId = "";
    private void requestData() {
        strategyPresenter.getMethodList(typeId, "0", pageNo, 30);
    }

    @Subscribe
    public void onTypeClick(PlayTypeBean typeBean) {
        typeId = typeBean.getId();
        mRefresh.autoRefresh();
    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    @Override
    protected void lazyLoad() {
        mAdapter = new RVAdapter<>(new StrategyTopItem(getActivity()), new CircleRecommendItem(getActivity()));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
        mRefresh.autoRefresh();
        strategyPresenter.getPlayType();
    }

    public static StrategyFragment newInstance() {
        StrategyFragment recommendFragment = new StrategyFragment();
        return recommendFragment;
    }


    @Override
    protected void initInject() {
        super.initInject();
        strategyPresenter = new StrategyPresenter();
        initPresenters(strategyPresenter);
    }

    @Override
    public void setStrategyListData(List<StrategyBean> strategyBeans, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore(pageNo < totalPage);
        List<Object> datas = mAdapter.getDatas();
        if (isRefresh) {
            datas.clear();
            datas.add(bean);
        }
        datas.addAll(strategyBeans);
        mAdapter.notifyDataSetChanged();

    }
}
